package com.palfund.broadcast;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

/**
 * priority:
 * OwnStaticReceiver 360
 * OwnDynamicReceiver 380(880)
 * StaticReceiver 660
 * DynamicReceiver 680
 * DynamicCancelReceiver 690
 * 静态注册的Receiver,不启动app也能接收到广播
 * 动态注册的Receiver在Activity销毁时,需要取消注册否则会报错(不取消注册也接收不到广播)
 * 动态注册的Receiver注册后才能收到广播,取消注册后便不能收到广播
 */

/**
 * 7.0
 * 广播的权限限制:
 * 1.谁有权收我的广播？:在自己应用发广播时添加参数声明Receiver所需的权限
 * A. manifests中声明新的权限:
 * <permission android:name="com.android.permission.RECEIVE_BROADCAST_PERMISSION"/>
 * B. 发送广播时将此权限作为参数传入:sendBroadcast(Intent intent,String receiverPermission)发送广播
 * 备注:这样做之后就使得只有具有RECV_XXX权限的Receiver才能接收此广播
 * C. 在Receiver应用的AndroidManifest.xml中要添加对应的RECEIVE_XXX权限。
 * <uses-permission android:name="com.android.permission.RECEIVE_BROADCAST_PERMISSION"/>
 * 2.谁有权给我发广播？(自己不受影响)
 * A. 首先同上，在AndroidManifest.xml中定义新的权限SEND_XXX
 * <permission android:name="com.android.SEND_XXX"/>
 * B. 然后，在Receiver app的Androidmanifest.xml中的<receiver>tag里添加权限SEND_XXX的声明
 * android:permission="com.android.permission.SEND_XXX"
 * 备注:该Receiver便只能接收来自具有该SEND_XXX权限的应用发出的广播。
 * C. 要发送这种广播，需要在Sender app的AndroidManifest.xml中也声明使用该权限即可
 * <uses-permission android:name="com.android.permission.SEND_XXX"></uses-permission>
 */
public class MainActivity extends AppCompatActivity {

    private OwnDynamicReceiver mOwnDynamicReceiver;
    private LocalBroadcastManager mLocalBroadcastManager;
    private LocalReceiver mLocalReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 1, 1, "send disOrder");
        menu.add(Menu.NONE, 2, 1, "register");
        menu.add(Menu.NONE, 3, 1, "UnRegister");
        menu.add(Menu.NONE, 4, 1, "register Local");
        menu.add(Menu.NONE, 5, 1, "send Local");
        menu.add(Menu.NONE, 6, 1, "unRegister Local");
        menu.add(Menu.NONE, 7, 1, "send Order");
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                //发送无序广播
                Intent intentDisOrder = new Intent();
                intentDisOrder.setAction("com.palfund.broadcast.receive");
                intentDisOrder.putExtra("info", "DisOrder");
                sendBroadcast(intentDisOrder);
                break;
            case 2:
                //注册动态广播
                mOwnDynamicReceiver = new OwnDynamicReceiver();
                IntentFilter dynamic = new IntentFilter();
                dynamic.addAction("com.palfund.broadcast.receive");
                dynamic.setPriority(380);
                registerReceiver(mOwnDynamicReceiver, dynamic);
                break;
            case 3:
                // 动态广播取消注册
                unregisterReceiver(mOwnDynamicReceiver);
                break;
            case 4:
                //注册本地广播
                mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
                mLocalReceiver = new LocalReceiver();
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction("com.palfund.broadcast.receive");
                intentFilter.setPriority(381);
                mLocalBroadcastManager.registerReceiver(mLocalReceiver, intentFilter);
                break;
            case 5:
                //发送本地广播,本地广播只能被LocalBroadcastManager注册的Receiver接收,
                //LocalBroadcastManager注册的Receiver只能接收本地广播
                Intent intentLocal = new Intent();
                intentLocal.setAction("com.palfund.broadcast.receive");
                intentLocal.putExtra("info", "local");
                mLocalBroadcastManager.sendBroadcast(intentLocal);
                break;

            case 6:
                //本地广播取消注册
                mLocalBroadcastManager.unregisterReceiver(mLocalReceiver);
                break;
            case 7:
                //发送有序广播
                Intent intentOrder = new Intent();
                intentOrder.setAction("com.palfund.broadcast.receive");
                intentOrder.putExtra("info", "order");
                sendOrderedBroadcast(intentOrder, null);
                // OwnStaticReceiver 360 OwnDynamicReceiver 380
                // StaticReceiver 660 DynamicReceiver 680 DynamicCancelReceiver 690
                // order (Dynamic   order) (Static   order) (Static   DynamicReceiver info   444)
                // (Static   DynamicReceiver   extra   ) (OwnDynamic   order) (OwnStatic   order)
                // (OwnStatic   OwnDynamic   info   444) (OwnStatic   DynamicReceiver   extra   )
                /*
                * order (OwnDynamic   order) (Dynamic   order) (Static   order)
                * (Static DynamicReceiver info   444) (Static   DynamicReceiver   extra   )
                * (OwnStatic   order) (OwnStatic   DynamicReceiver   info   444)
                * (OwnStatic DynamicReceiver   extra   )
                *
                * */
                break;
            default:

                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
